package src; 
import java.awt.Color;
import java.util.*;

//The subGrid class keeps track of a single Tic-Tac-Toe game within the larger mainGrid. 
//The rules of regular Tic-Tac-Toe should apply here. 
public class SubGrid 
{
	private GamePiece[][] subGrid; 
	private int mainRow;
	private int mainCol; 
	
	private boolean hasWinner; 
	
	public SubGrid( int mgr, int mgc )
	{
		subGrid = new GamePiece[3][3];
		mainRow = mgr; 
		mainCol = mgc; 
		hasWinner = false; 
	}
	
	
	/**
	 * Make sure doesn't have winner and that not tie (all spots taken)
	 * @return boolean whether or not subGrid has playable spots
	 */
	public boolean canPlay()
	{
		return !hasWinner && getOpenLocations().size() != 0; 
	}
	
	/**
	 * Iterates through subgrid, returns locations without GamePiece
	 * @return ArrayList of open locations in this grid
	 */
	public ArrayList<GridLocation> getOpenLocations()
	{
		ArrayList<GridLocation> open = new ArrayList<GridLocation>();
		
		for( int i = 0; i < subGrid.length; i++ )
		{
			for( int j = 0; j < subGrid[i].length; j++ )
			{
				if( subGrid[i][j] == null )
				{
					GridLocation loc = new GridLocation(mainRow, mainCol, i, j);
					open.add(loc);
				}
			}
		}
				
		return open;
	}
	
	/**
	 * For use in Board
	 * @return if board has been won
	 */
	public boolean hasWinner()
	{
		return hasWinner; 
	}
	
	/**
	 * Checks if there is a winner, to be called after player makes move
	 * @return boolean whether or not winner
	 */
	public boolean checkForWinner()
	{
		boolean winner = false; 
		GamePiece piece; 
		
		//First, check for 3 consecutive in row
		for( int row = 0; row < subGrid.length; row++ )
		{
			winner = true; 
			
			if( subGrid[row][0] != null )
			{
				piece = subGrid[row][0];
				
				for( int col = 1; col < subGrid[row].length; col++ )
				{
					if(subGrid[row][col] == null || !piece.equals(subGrid[row][col]))
					{
						winner = false; 
					}
				}
				
				if( winner )
				{
					hasWinner = true;
					colorBoard(piece.getColor());
					return true; 
				}
			}
			else
			{
				winner = false; 
			}
			
		}
		
		//Then, check for three in col
		for( int col = 0; col < subGrid[0].length; col++ )
		{
			winner = true; 
			
			if( subGrid[0][col] != null )
			{
				piece = subGrid[0][col];
				
				for( int row = 1; row < subGrid.length; row++ )
				{
					if(subGrid[row][col] == null || !piece.equals(subGrid[row][col]))
					{
						winner = false; 
					}
				}
				
				if( winner )
				{
					hasWinner = true;
					colorBoard(piece.getColor());
					return true; 
				}
			}
			else
			{
				winner = false; 
			}
			
			
		}		
		
		//Then, check first diagonal (going down left to right)
		if( subGrid[0][0] != null )
		{
			piece = subGrid[0][0];
			winner = true; 
			int row = 1; 
			int col = 1; 
			
			while( winner && row < subGrid.length && col < subGrid[row].length)
			{
				if( subGrid[row][col] != null && piece.equals(subGrid[row][col]))
				{
					row++;
					col++;
				}
				else
				{
					winner = false; 
					break; 
				}
			}
			
			if( winner )
			{
				hasWinner = true;
				colorBoard(piece.getColor());
				return true; 
			}
			
		}
		
	
		
		//Then, check other diagonal (going up left to right)
		if( subGrid[subGrid.length - 1][0] != null )
		{
			piece = subGrid[subGrid.length - 1][0];
			winner = true; 
			int row = 1; 
			int col = 1; 
			
			
			while( winner && row >= 0 && col < subGrid[row].length)
			{				
				if( subGrid[row][col] != null && piece.equals(subGrid[row][col]))
				{
					row--;
					col++;
				}
				else
				{
					winner = false; 
					break; 
				}
			}
			
			if( winner )
			{
				hasWinner = true;
				colorBoard(piece.getColor());
				return true; 
			}
			
		}
		return false; 
	}
	
	/**
	 * @param c winner's color
	 * Fills all spots on the board with winner's game piece color. Called when win
	 */
	public void colorBoard(Color c)
	{
		if( hasWinner )
		{
			System.out.println("WINNER FOR THIS BOARD"); //temp
			
			for( int i = 0; i < subGrid.length; i++)
			{
				for( int j = 0; j < subGrid[i].length; j++)
				{
					if( subGrid[i][j] == null )
					{
						subGrid[i][j] = new GamePiece(c, new GridLocation( mainRow, mainCol, i, j));
					}
					else
					{
						subGrid[i][j].setColor(c);
					}
				}
			}
		}
		
	}
	
	
	/**
	 * To be used in MainGrid, returns color of winner
	 * @return color of subGrid winner
	 */
	public Color getWinnerColor()
	{
		if( hasWinner )
		{
			return subGrid[0][0].getColor();
		}
		else
		{
			return null; 
		}
	}
	
	public int getMainRow()
	{
		return mainRow;
	}
	
	public int getMainCol()
	{
		return mainCol; 
	}
	
	public int getLength()
	{
		return subGrid.length; 
	}
	
	public int getLength( int row )
	{
		if( row < subGrid.length )
		{
			return subGrid[row].length; 
		}
		
		return 0; 
	}
	
	public GamePiece get(int row, int col)
	{
		if( row < subGrid.length && col < subGrid[row].length && subGrid[row][col] != null )
		{
			return subGrid[row][col];
		}
		else
		{
			return null; 
		}
	}
	
	public void set(int row, int col, GamePiece p)
	{
		subGrid[row][col] = p; 
	}
	
	
	
	
	
	
	
}
